package org.example.impl;

import org.example.clientSendRead;
import org.example.pojo.clientInfo;
import org.example.tools.utilsTools;

import java.io.InputStream;
import java.net.Socket;
import java.util.Map;
import java.util.Set;

/**
 * 获取当前发送者的消息
 */
public class clientDataMessageImpl implements Runnable, clientSendRead {
    @Override
    public void run() {
        while (true){
             synchronized (this){
                 try {
                     Set<Map.Entry<Socket, clientInfo>> infoSet = utilsTools.UserMaps.entrySet();
                     utilsTools.selectSocketMaps(infoSet,this);
                 }catch (Exception e){
                     break;
                 }
             }
        }
    }

    /**
     * @param socketMap 这个获取当前的消息聚到
     */
    @Override
    public void SocketData(Set<Map.Entry<Socket, clientInfo>> socketMap) {
       try {
           for (Map.Entry<Socket,clientInfo> socketClientInfoEntry:socketMap){
               InputStream inputStream = socketClientInfoEntry.getKey().getInputStream();
               if(inputStream.available() > 0){
                   byte[] byTsy = utilsTools.SocketDataByte(inputStream);
                   if( byTsy!=null){
                       clientSelectDataMapsImpl csDmi = new clientSelectDataMapsImpl(socketClientInfoEntry,byTsy);
                       csDmi.startSelect();
                   }
               }
           }
       }catch (Exception e){}
    }
}
